package model

import (
	"fmt"
	"path/filepath"

	"gitee.com/liumou_site/gf"
	"gitee.com/liumou_site/logger"
)

func (api *Server) GetIndexHtml(ip, bg, url, ver string) string {
	backgroundImage := "bg1.jpg" // 默认背景图片
	
	// 只有当用户显式传入背景图标且文件存在时才使用外部文件
	if bg != "" {
		f := gf.NewFile(bg)
		f.IsFile()
		if f.IsFiles {
			// 检查是否是绝对路径
			if filepath.IsAbs(bg) {
				backgroundImage = bg
			} else {
				// 复制文件到临时目录或直接使用相对路径
				backgroundImage = filepath.Base(bg)
			}
			logger.Info("使用自定义背景图标: ", bg)
		} else {
			logger.Warn("指定的背景图标文件不存在，使用内置背景: ", bg)
		}
	}
	
	text := fmt.Sprintf(`<!doctype html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>IP查询</title>
  <link href="/static/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
  <script src="/static/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
</head>
<body style="background-image: url('/static/%s'); background-repeat: no-repeat;background-size: cover;">
<style>
  .container{
    height: 95vh;
    text-align: center;
    padding-top: 46vh;
  }
  .flex-container {
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
<div class="container">
  <div class="btn btn-success" style="margin: auto;">
    <p>你的IP: %s</p>
  </div>
    <div class="flex-container">
      <p class="btn btn-outline-light">
        <a href="/json">Json格式</a>
      </p>

      <p class="btn btn-outline-light">
        <a href="/api">文本格式</a>
      </p>
    </div>

    <div class="flex-container">
      <p class="btn btn-outline-light">
        <a href="%s">IPV%s查询</a>
      </p>
      <p class="btn btn-outline-light">
        <a href="https://gitee.com/liumou_site/go-ip-interface">项目主页</a>
      </p>
    </div>

    <div class="flex-container">
      <p class="btn btn-outline-light">
        <a href="http://speed.nuaa.edu.cn">南京航空航天大学测速[4&6]</a>
      </p>
      <p class="btn btn-outline-light">
        <a href="https://test.ustc.edu.cn/">中科大测速[4&6]</a>
      </p>
    </div>

  <div class="btn btn-outline-light" style="margin: auto;">
    <a href="https://liumou.site/">作者博客</a>
  </div>
</div>

</body>

</html>
`, backgroundImage, ip, url, ver)
	if api.Debug {
		fmt.Println(text)
	}
	return text
}
